package company.bigo_F;

import java.util.HashMap;
import java.util.Map;

//设计思路
//通过重试，一旦当前服务器空闲则立即使用，否则切换到下一台服务器
//在Map存放weight个的server（指向count个的服务器），可以保证按照权重分配服务器任务
public class BiGo3 {

    private int weight;

    private int count;

    private Map<Integer, Server> servers;

    public BiGo3() {
        weight = 0;
        count = 0;
        servers = new HashMap<Integer, Server>();
    }

    //增加服务器
    public void addServer(Server s) {
        count++;
        for (int i = 0; i < s.getWeight(); i++) {
            weight++;
            servers.put(weight, s);
        }
    }

    //处理服务
    public void handlerServer(int requestId) {
        int serverId = requestId % weight;
        Server s = servers.get(serverId);
        while (s.isBusy()) {
            s = servers.get(serverId++);
        }
        s.doRequest(requestId);
    }

    public int getCount() {
        return count;
    }
}

class Server {

    private String serverId;

    private int weight;

    private boolean isBusy;

    public Server(String serverId, int weight, boolean isBusy) {
        this.serverId = serverId;
        this.weight = weight;
        this.isBusy = isBusy;
    }

    public String getServerId() {
        return serverId;
    }

    public void setServerId(String serverId) {
        this.serverId = serverId;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public boolean isBusy() {
        return isBusy;
    }

    public void setBusy(boolean busy) {
        isBusy = busy;
    }

    public void doRequest(int requestId) {
        // finish request
    }
}
